//
//  FlipsideViewController.m
//  RunSketch
//
//  Created by Brian Hinch on 7/8/08.
//  Copyright Tellart 2008. All rights reserved.
//

#import "FlipsideViewController.h"


@implementation FlipsideViewController

- (void)viewDidLoad {
	self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
	// Return YES for supported orientations
	return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
	// Release anything that's not essential, such as cached data
}

/*
 * returns the server address entered by the user
 */
-(id)getSketchServerNameInUI {
    return sketchServerTextField.text;
}

/*
 * sets the server address displayed in the text field
 */
-(void)setSketchServerNameInUI: (NSString*) text {
	sketchServerTextField.text = text;
}

/*
 * retruns the state of the flow control switch in the UI
 */
- (int)getFlowControlStateInUI {
	if (flowControlSwitch.on == YES) {
		return 1;
	} else {
		return 0;
	}
}

/*
 * sets the state of the flow control switch in the UI
 */
- (void)setFlowControlStateInUI:(int)state {
	if (state == 1) {
		flowControlSwitch.on = YES;
	} else {
		flowControlSwitch.on = NO;
	}
}

- (int)getIOPackBoostStateInUI {
	if (iopackBoostSwitch.on == YES) {
		return 1;
	} else {
		return 0;
	}
}
- (void)setIOPackBoostStateInUI:(int)state {
	if (state == 1) {
		iopackBoostSwitch.on = YES;
	} else {
		iopackBoostSwitch.on = NO;
	}
}

/*
/*
 * hide the update controls and display the cancel controls 
 */
-(void)showCancelUI {
    [sketchServerTextField setEnabled:NO];
    [updateSketchButton setHidden:YES];
    [updatingLabel setHidden:NO];
    [updateProgress setHidden:YES];
    [updateActivity setHidden:NO];
    [cancelUpdateButton setHidden:NO];
}

/*
 * hide the cancel controls and display the update controls 
 */
-(void)hideCancelUI {
    [updatingLabel setHidden:YES];
    [updateActivity setHidden:YES];
    [updateProgress setHidden:YES];
    [cancelUpdateButton setHidden:YES];
    [sketchServerTextField setEnabled:YES];
    [updateSketchButton setHidden:NO];
}

/*******************************************************************
 */
-(void) didStartSketchUpdate {
    [self showCancelUI];
    [updatingLabel setText:@"Connecting...     "];
}
-(void) didFailWithConnectionError: (SketchUpdateController *)c {
    NSLog(@"FlipsideViewController: didFailWith");
    [self hideCancelUI];
    [updatingLabel setText:@"Connection error"];
    [updatingLabel setHidden:NO];
}
-(void) didFailWithCommunicationError: (SketchUpdateController *)c:(NSString *)msg {
    NSLog(msg);
    [self hideCancelUI];
    [updatingLabel setText:@"Communication error"];
    [updatingLabel setHidden:NO];
}
-(void) didLoadCatalog: (SketchUpdateController *)c {
    NSLog(@"FlipsideViewController: catalogDidLoad");
    [updatingLabel setText:@"Downloading...     "];
}
-(void) didFailWithCatalogParserError: (SketchUpdateController *)c: (NSError *)err {
    NSLog([NSString stringWithFormat:@"FlipsideViewController: didFailWithCatalogParserError: %@", [err localizedDescription]]);
    [self hideCancelUI];
    [updatingLabel setText:@"Invalid catalog"];
    [updatingLabel setHidden:NO];
}
-(void) madeProgress: (SketchUpdateController *)c: (int)bytesLoaded: (int)bytesTotal {
    float prog = (float)bytesLoaded / (float)bytesTotal;
    NSLog([NSString stringWithFormat:@"FlipsideViewController: madeProgress %i / %i = %f", bytesLoaded, bytesTotal, prog]);
    if ((prog >= 0) && (prog < 1.0)) {
        [updateProgress setHidden:NO];
        [updateActivity setHidden:YES];
        [updateProgress setProgress:prog];
    }
}
-(void) didSucceed: (SketchUpdateController *)c {
    NSLog(@"FlipsideViewController: didSucceed");
    NSLog(@"FlipsideViewController: hiding cancel UI");
    [self hideCancelUI];
    [updatingLabel setText:@"Sketch opened."];
    [updatingLabel setHidden:NO];
}
-(void) didStop: (SketchUpdateController *)c {
    NSLog(@"FlipsideViewController: didStop");
    NSLog(@"FlipsideViewController: hiding cancel UI");
    [self hideCancelUI];
    [updatingLabel setText:@"Open cancelled."];
    [updatingLabel setHidden:NO];
}
/*******************************************************************/


- (void)dealloc {
	[super dealloc];
}


@end
